package com.ld.rightsSystem.controller;


import com.ld.rightsSystem.Enum.ResponseEnum;
import com.ld.rightsSystem.dto.TUserDTO;
import com.ld.rightsSystem.exception.DBDataNotFoundException;
import com.ld.rightsSystem.pojo.TUser;
import com.ld.rightsSystem.vo.R;
import com.ld.rightsSystem.service.inf.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author lhb
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/admin/user")
public class TUserController {
    @Autowired
    private TUserService tUserService;

    /**
     * 添加用户
     * @param tUserDTO
     * @return
     */
    @PostMapping("/add")
    public R addSTUser(@RequestBody TUserDTO tUserDTO){
        return tUserService.addTUser(tUserDTO);
    }

    /**
     * 分页查询用户
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/query")
    public R getTUser(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                      @RequestParam(value = "pageSize",required = false)Integer pageSize,
                      @RequestParam(value = "checkUName",required = false) String checkUName,
                      @RequestParam(value = "userId",required = false) Long userId){
        if(userId != null){
            return tUserService.findTUserById(userId);
        }else if(pageNum != null && pageSize != null){
            return tUserService.getTUser(pageNum,pageSize);
        }else if(checkUName != null){
            return tUserService.checkFieldAvailable(checkUName);
        }else {
            return new R().error(ResponseEnum.PARAMS_BINDING_ERROR);
        }
    }

    /**
     * 根据id删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/delete")
    public R deleteTUserById(@RequestParam("id") Long userId) throws DBDataNotFoundException {
        return tUserService.deleteTUserById(userId);
    }

    /**
     * 修改用户
     * @param tUserDTO
     * @return
     */
    @PutMapping("/update")
    public R editTUser(@RequestBody TUserDTO tUserDTO) throws DBDataNotFoundException {
        return tUserService.editTUser(tUserDTO);
    }

}

